#include "setting.h"
#include "ui_setting.h"
#include "config/config.h"
#include "config/constants.h"
#include "util/utils.h"

#include <QMessageBox>

Setting::Setting(BaseForm *parent) :
    BaseForm(parent),
    ui(new Ui::Setting)
{
    ui->setupUi(this);
    mymenu->hide();

    initWindow();
}

Setting::~Setting()
{
    delete ui;
}

void Setting::setUpdateMainCallback(std::function<void ()> _callBack)
{
    this->updateMainCallback = _callBack;
}

void Setting::initWindow()
{
    readTestParamWidget();
    readTestFTD();
    readTestProtect();
    readTestInfo();
}

void Setting::on_btnSetttingOK_clicked()
{
    if(ui->tabWidget->currentIndex() == 0)
    {
        saveTestParamWidget();
    }
    else if(ui->tabWidget->currentIndex() == 1)
    {
        saveTestFTD();
    }
    else if(ui->tabWidget->currentIndex() == 2)
    {
        saveTestProtect();
    }
    else if(ui->tabWidget->currentIndex() == 3)
    {
        saveTestInfo();
    }
}

void Setting::on_btnSetttingCancel_clicked()
{
    this->close();
}

void Setting::saveTestParamWidget()
{
    LOG_INFO << "Save Test Param.";

    int sel = QMessageBox::information(this, "提示", "是否保存当前设置！", QMessageBox::Yes | QMessageBox::No);
    if(sel == QMessageBox::No)
    {
        return;
    }


    Config cfg;
    auto flag = ui->rbSettingTest1->isChecked();
    if(flag)
    {
        cfg.Set(TEST_PARAM_NAME, TEST_PARAM_SELECT, flag);
    }
    else
    {
        cfg.Set(TEST_PARAM_NAME, TEST_PARAM_SELECT, false);
    }


    auto flag1 = ui->rbSettingCtrl1->isChecked();
    if(flag1)
    {
        cfg.Set(TEST_PARAM_NAME, TEST_PARAM_CTRL_STYLE, flag1);
    }
    else
    {
        cfg.Set(TEST_PARAM_NAME, TEST_PARAM_CTRL_STYLE, false);
    }

    cfg.Set(TEST_PARAM_NAME, TEST_PARAM_FREQ, ui->leSettingRate->text().toFloat());
    cfg.Set(TEST_PARAM_NAME, TEST_PARAM_SPEED, ui->leSettingTestSpeed->text().toInt());

    cfg.Set(TEST_PARAM_NAME, TEST_PARAM_NUM, ui->leSettingNum->text().toInt());

    cfg.Set(TEST_PARAM_NAME, TEST_PARAM_RADIS, ui->leSettingRadius->text().toInt());

    cfg.Set(TEST_PARAM_NAME, TEST_PARAM_DOWN, ui->leSettingSchedule->text().toInt());

    cfg.Set(TEST_PARAM_NAME, TEST_PARAM_UP, ui->leSettingScheduleLoad->text().toInt());

    cfg.Set(TEST_PARAM_NAME, TEST_PARAM_PRELOAD, ui->leSettingCharge->text().toFloat());

    cfg.Set(TEST_PARAM_NAME, TEST_PARAM_LOAD, ui->leSettingChargeLoad->text().toFloat());

    cfg.Set(TEST_PARAM_NAME, TEST_PARAM_KEEP_TIME, ui->leSettingTime->text().toInt());

    auto flag2 = ui->rbSettingCharge1->isChecked();
    if(flag2)
    {
        cfg.Set(TEST_PARAM_NAME, TEST_PARAM_KEEP_LOAD, flag2);
    }
    else
    {
        cfg.Set(TEST_PARAM_NAME, TEST_PARAM_KEEP_LOAD, false);
    }

    QMessageBox::information(this, "提示", "保存成功！", QMessageBox::Yes);

    updateMainCallback();
}

void Setting::saveTestFTD()
{
    LOG_INFO << "Save Test FTD Param.";
    int sel = QMessageBox::information(this, "提示", "是否保存当前设置！", QMessageBox::Yes | QMessageBox::No);
    if(sel == QMessageBox::No)
    {
        return;
    }


    Config cfg;
}

void Setting::saveTestProtect()
{
    LOG_INFO << "Save Test Protect Param.";
    int sel = QMessageBox::information(this, "提示", "是否保存当前设置！", QMessageBox::Yes | QMessageBox::No);
    if(sel == QMessageBox::No)
    {
        return;
    }


    Config cfg;
    cfg.Set(TEST_PARAM_PROTECT, TEST_PARAM_PROTECT_DIS_UP, ui->pbSettingShift1->text().toInt());
    cfg.Set(TEST_PARAM_PROTECT, TEST_PARAM_PROTECT_DIS_DOWN, ui->pbSettingShift2->text().toInt());
    cfg.Set(TEST_PARAM_PROTECT, TEST_PARAM_PROTECT_LOAD_UP, ui->pbSettingCharge1->text().toInt());
    cfg.Set(TEST_PARAM_PROTECT, TEST_PARAM_PROTECT_LOAD_DOWN, ui->pbSettingCharge2->text().toInt());

    QMessageBox::information(this, "提示", "保存成功！", QMessageBox::Yes);
}

void Setting::saveTestInfo()
{
    LOG_INFO << "Save Test Information.";
}

void Setting::readTestParamWidget()
{
    LOG_INFO << "=====readTestParamWidget====";

    Config cfg;
    QVariant val = cfg.Get(TEST_PARAM_NAME, TEST_PARAM_SELECT);
    if(val.isValid())
    {
        auto tempValue = val.toBool();
        if(tempValue)
        {
            ui->rbSettingTest1->setChecked(Qt::Checked);
            ui->rbSettingTest2->setChecked(Qt::Unchecked);
        }
        else
        {
            ui->rbSettingTest1->setChecked(Qt::Unchecked);
            ui->rbSettingTest2->setChecked(Qt::Checked);
        }

        val = cfg.Get(TEST_PARAM_NAME, TEST_PARAM_CTRL_STYLE);
        tempValue = val.toBool();
        if(tempValue)
        {
            ui->rbSettingCtrl1->setChecked(Qt::Checked);
            ui->rbSettingCtrl2->setChecked(Qt::Unchecked);
        }
        else
        {
            ui->rbSettingCtrl1->setChecked(Qt::Unchecked);
            ui->rbSettingCtrl2->setChecked(Qt::Checked);
        }

        val = cfg.Get(TEST_PARAM_NAME, TEST_PARAM_FREQ);
        tempValue = val.toFloat();
        ui->leSettingRate->setText(Utils::formatFloat(val.toFloat()));

        val = cfg.Get(TEST_PARAM_NAME, TEST_PARAM_SPEED);
        tempValue = val.toInt();
        ui->leSettingTestSpeed->setText(QString::number(val.toInt()));

        val = cfg.Get(TEST_PARAM_NAME, TEST_PARAM_NUM);
        tempValue = val.toInt();
        ui->leSettingNum->setText(QString::number(val.toInt()));

        val = cfg.Get(TEST_PARAM_NAME, TEST_PARAM_RADIS);
        tempValue = val.toInt();
        ui->leSettingRadius->setText(QString::number(val.toInt()));

        val = cfg.Get(TEST_PARAM_NAME, TEST_PARAM_DOWN);
        tempValue = val.toInt();
        ui->leSettingSchedule->setText(QString::number(val.toInt()));

        val = cfg.Get(TEST_PARAM_NAME, TEST_PARAM_UP);
        tempValue = val.toInt();
        ui->leSettingScheduleLoad->setText(QString::number(val.toInt()));

        val = cfg.Get(TEST_PARAM_NAME, TEST_PARAM_PRELOAD);
        tempValue = val.toFloat();
        ui->leSettingCharge->setText(Utils::formatFloat(val.toFloat()));

        val = cfg.Get(TEST_PARAM_NAME, TEST_PARAM_LOAD);
        tempValue = val.toFloat();
        ui->leSettingChargeLoad->setText(Utils::formatFloat(val.toFloat()));

        val = cfg.Get(TEST_PARAM_NAME, TEST_PARAM_KEEP_LOAD);
        tempValue = val.toBool();
        if(val.toBool())
        {
            ui->rbSettingCharge1->setChecked(Qt::Checked);
            ui->rbSettingCharge2->setChecked(Qt::Unchecked);
        }
        else
        {
            ui->rbSettingCharge1->setChecked(Qt::Unchecked);
            ui->rbSettingCharge2->setChecked(Qt::Checked);
        }

        val = cfg.Get(TEST_PARAM_NAME, TEST_PARAM_KEEP_TIME);
        tempValue = val.toInt();
        ui->leSettingTime->setText(QString::number(val.toInt()));
    }
    else
    {
        ui->rbSettingTest1->setChecked(Qt::Unchecked);
        ui->rbSettingTest2->setChecked(Qt::Checked);
        cfg.Set(TEST_PARAM_NAME, TEST_PARAM_SELECT, paramModel.testMethod);

        ui->rbSettingCtrl1->setChecked(Qt::Unchecked);
        ui->rbSettingCtrl2->setChecked(Qt::Checked);
        cfg.Set(TEST_PARAM_NAME, TEST_PARAM_CTRL_STYLE, paramModel.testMethod);

        ui->rbSettingCharge1->setChecked(Qt::Unchecked);
        ui->rbSettingCharge2->setChecked(Qt::Checked);
        cfg.Set(TEST_PARAM_NAME, TEST_PARAM_KEEP_LOAD, paramModel.isKeepLoad);

        cfg.Set(TEST_PARAM_NAME, TEST_PARAM_FREQ, paramModel.testFreq);
        ui->leSettingRate->setText(Utils::formatFloat(paramModel.testFreq));

        cfg.Set(TEST_PARAM_NAME, TEST_PARAM_SPEED, paramModel.testSpeed);
        ui->leSettingTestSpeed->setText(QString::number(paramModel.testSpeed));

        cfg.Set(TEST_PARAM_NAME, TEST_PARAM_NUM, paramModel.testNum);
        ui->leSettingNum->setText(QString::number(paramModel.testNum));

        cfg.Set(TEST_PARAM_NAME, TEST_PARAM_RADIS, paramModel.radis);
        ui->leSettingRadius->setText(QString::number(paramModel.radis));

        cfg.Set(TEST_PARAM_NAME, TEST_PARAM_DOWN, paramModel.downVal);
        ui->leSettingSchedule->setText(QString::number(paramModel.downVal));

        cfg.Set(TEST_PARAM_NAME, TEST_PARAM_UP, paramModel.upVal);
        ui->leSettingScheduleLoad->setText(QString::number(paramModel.upVal));

        cfg.Set(TEST_PARAM_NAME, TEST_PARAM_PRELOAD, paramModel.preLoad);
        ui->leSettingCharge->setText(Utils::formatFloat(paramModel.preLoad));

        cfg.Set(TEST_PARAM_NAME, TEST_PARAM_LOAD, paramModel.load);
        ui->leSettingChargeLoad->setText(Utils::formatFloat(paramModel.load));
    }
}

void Setting::readTestFTD()
{
    LOG_INFO << "=====readTestFTD====";

}

void Setting::readTestProtect()
{
    LOG_INFO << "=====readTestProtect====";
    Config cfg;
    QVariant val = cfg.Get(TEST_PARAM_PROTECT, TEST_PARAM_PROTECT_DIS_UP);
    if(val.isValid())
    {
        ui->pbSettingShift1->setValue(val.toInt());
        val = cfg.Get(TEST_PARAM_PROTECT, TEST_PARAM_PROTECT_DIS_DOWN);
        ui->pbSettingShift2->setValue(val.toInt());
        val = cfg.Get(TEST_PARAM_PROTECT, TEST_PARAM_PROTECT_LOAD_UP);
        ui->pbSettingCharge1->setValue(val.toInt());
        val = cfg.Get(TEST_PARAM_PROTECT, TEST_PARAM_PROTECT_LOAD_DOWN);
        ui->pbSettingCharge2->setValue(val.toInt());
    }
    else
    {
        cfg.Set(TEST_PARAM_PROTECT, TEST_PARAM_PROTECT_DIS_UP, ui->pbSettingShift1->text().toInt());
        cfg.Set(TEST_PARAM_PROTECT, TEST_PARAM_PROTECT_DIS_DOWN, ui->pbSettingShift2->text().toInt());
        cfg.Set(TEST_PARAM_PROTECT, TEST_PARAM_PROTECT_LOAD_UP, ui->pbSettingCharge1->text().toInt());
        cfg.Set(TEST_PARAM_PROTECT, TEST_PARAM_PROTECT_LOAD_DOWN, ui->pbSettingCharge2->text().toInt());
    }
}

void Setting::readTestInfo()
{
    LOG_INFO << "=====readTestInfo====";
}
